package ua.epam.muzalevskiy.traction.dao;

import java.sql.SQLException;

/**
 * This class needed for creating various <code> ConnectionFactory </code>
 * objects.
 *
 * @author Muzalevskiy Vitaliy
 */
public class ConnectionFactoryFactory {
    
    /**
     * Enumeration of all available ConnectionFactories.
     */
    public static enum FactoryType{ WITHOUT_POOL, C3P0}
    
    private static FactoryType currentType = FactoryType.WITHOUT_POOL;
    
    /**
     * Changing <code> ConnectionFactory </code> type which will be used to
     * connect to DataBase.
     *
     * @param type - type of used <code> ConnectionFactory </code>.
     */
    public static synchronized void setType(FactoryType type){
        currentType = type;
    }
    
    /**
     * This method create new <code> ConnectionFactory </code>. By default
     * it use <code> ConnectionFactory </code> without Connection Pool.
     *
     * @return <code> ConnectionFactory </code> object.
     */
    public synchronized static ConnectionFactory newConnectionFactory(){
        ConnectionFactory result;
        
        try {
            switch(currentType) {
                case WITHOUT_POOL:
                    result = new ConnectionFactoryJdbc();
                    break;
                case C3P0:
                    result = new ConnectionFactoryC3P0();//.getReference();
                    break;
                default:
                    throw new RuntimeException("Error");
            }
        } catch(SQLException e){
            throw new RuntimeException(e);
        }
        return result;
    }
}
